package com.yt.website.action;

import com.yt.core.dao.PagingBean;
import com.yt.core.dao.filter.DynamicSQLFilterImpl;
import com.yt.core.dao.filter.QueryFilter;
import com.yt.website.api.interfaces.IndexSaleService;
import com.yt.website.api.interfaces.NewsService;
import com.yt.website.api.model.News;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/news")
public class NewsAction {

	private final Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private NewsService newsService;


	@RequestMapping("/index")
	public ModelAndView index(@RequestParam(value = "pageNo", required = false) Integer pageNo) {
		ModelAndView mav = new ModelAndView("news/News");
		try {
			int currentPage = pageNo == null ? 1 : pageNo;
			QueryFilter filter = new DynamicSQLFilterImpl();
			filter.setPagingBean(new PagingBean((currentPage - 1) * 25, 25));
			filter.setSortField("id", true);
			List<News> userList = newsService.find(filter);
			int userCount = newsService.count(filter);
			mav.addObject("list", userList);
			pageNo=pageNo==null?1:pageNo;
			mav.addObject("pageNo", pageNo);
			mav.addObject("total", userCount);
		} catch (Exception e) {
			logger.error("news出错：{}", e.getMessage(), e);
		}
		return mav;
	}

	@RequestMapping("/detail")
	public ModelAndView index(@RequestParam(value = "id") Long id) {
		ModelAndView mav = new ModelAndView("news/NewsDetail");
		try {
			News news = newsService.get(id);
			mav.addObject("bean", news);
		} catch (Exception e) {
			logger.error("news出错：{}", e.getMessage(), e);
		}
		return mav;
	}
}
